package com.augurit.gzcgwsp.readAndParseXML.service.impl;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.gzcgwsp.jzfqwczzxn.service.IYwYnJzfqwczzXnyqService;
import com.augurit.gzcgwsp.jzfqwczzxn.web.form.YwYnJzfqwczzXnForm;
import com.augurit.gzcgwsp.readAndParseXML.service.IParseXMLService;
import com.augurit.gzcgwsp.readAndParseXML.service.IXnYqService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;

/**
 * 消纳 延期
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional
public class XnYqServiceImpl implements IXnYqService{
	Logger logger = (Logger) LoggerFactory.getLogger(XnYqServiceImpl.class);
	@Autowired
	private IParseXMLService parseXMLService;

	@Autowired
	private IYwYnJzfqwczzXnyqService ywYnJzfqwczzXnyqService;
	
	//读取接口配置信息
	Locale locale = Locale.getDefault(); 
	ResourceBundle localResource = ResourceBundle.getBundle("interfaceExchange", locale); 
	// xml存放路径
	private final String xnYqfilepath = localResource.getString("xnYqfilepath");

	// 附件存放路径
	private final String xnYqfujianpath = localResource.getString("xnYqfujianpath");

	// 解析成功反馈xml存放路径
	private final String xnYqfeedbackPath = localResource.getString("xnYqfeedbackPath");

	// 解析异常xml存放路径
	private final String xnYqexceptionPath = localResource.getString("xnYqexceptionPath");

	// 解析后，xml备份
	private final String xnYqbackPath = localResource.getString("xnYqbackPath");

	private Map<String, String> xnYqKeyMap;// xml内容的key

	private Map<String, String> xnYqKeyMapHead;// xml头部的key

	private Map<String, Long> xnYqfujianMap;// 附件的别名和id对应的map

	private String errorCode = "";// 异常编号

	private String errorStatus = "";// 异常描述

	private String errorStack = "";// 异常堆栈

	private void initxnYqMap() {
		// 初始化数据部分对应的map
		xnYqKeyMap = new HashMap<String, String>();
		xnYqKeyMap.put("approvalNo", "approvalNo");
		xnYqKeyMap.put("businessCode", "businessCode");
		xnYqKeyMap.put("YSL_NO", "YSL_NO");
		xnYqKeyMap.put("sendTime", "sendTime");
		
		xnYqKeyMap.put("GCDZ", "GCDZ");
		xnYqKeyMap.put("SQDW", "SQDW");
		xnYqKeyMap.put("GCMC", "GCMC");
		xnYqKeyMap.put("JSDW_DZ", "JSDW_DZ");
		xnYqKeyMap.put("JSDW_LXR", "JSDW_LXR");
		xnYqKeyMap.put("JSDW_LXR_LXDH", "JSDW_LXR_LXDH");
		xnYqKeyMap.put("SGDW_GZ", "SGDW_GZ");
		xnYqKeyMap.put("SGDW_XMJL", "SGDW_XMJL");
		xnYqKeyMap.put("SGDW_XMJL_LXDH", "SGDW_XMJL_LXDH");
		xnYqKeyMap.put("SGDW_LXR", "SGDW_LXR");
		xnYqKeyMap.put("SGDW_LXR_LXDH", "SGDW_LXR_LXDH");
		xnYqKeyMap.put("SBJZFQWCZL", "SBJZFQWCZL");
		

		// 初始化头部对应的map
		xnYqKeyMapHead = new HashMap<String, String>();
		xnYqKeyMapHead.put("sn", "sn");
		xnYqKeyMapHead.put("sender", "sender");
		// xnYqKeyMapHead.put("receiver", "receiver");
		xnYqKeyMapHead.put("businessCode", "businessCode");

		// 初始化附件的别名和id对应的map
		xnYqfujianMap = new HashMap<String, Long>();
		xnYqfujianMap.put("FJ_01", 728l);
		xnYqfujianMap.put("FJ_02", 729l);
		xnYqfujianMap.put("FJ_03", 730l);
		xnYqfujianMap.put("FJ_04", 731l);
		xnYqfujianMap.put("FJ_05", 732l);
		xnYqfujianMap.put("FJ_06", 733l);
		xnYqfujianMap.put("FJ_07", 734l);
		xnYqfujianMap.put("FJ_08", 735l);
		xnYqfujianMap.put("FJ_09", 736l);
		xnYqfujianMap.put("FJ_10", 737l);
		xnYqfujianMap.put("FJ_11", 738l);
		xnYqfujianMap.put("FJ_12", 739l);
		xnYqfujianMap.put("FJ_13", 740l);
		xnYqfujianMap.put("FJ_14", 741l);
		xnYqfujianMap.put("FJ_15", 742l);
		xnYqfujianMap.put("FJ_16", 743l);
		xnYqfujianMap.put("FJ_17", 744l);
		xnYqfujianMap.put("FJ_18", 745l);
		xnYqfujianMap.put("FJ_19", 746l);
		xnYqfujianMap.put("FJ_20", 747l);
		xnYqfujianMap.put("FJ_21", 748l);
		xnYqfujianMap.put("FJ_22", 749l);
		xnYqfujianMap.put("FJ_23", 750l);
		xnYqfujianMap.put("FJ_24", 751l);
		xnYqfujianMap.put("FJ_25", 752l);
		xnYqfujianMap.put("FJ_26", 753l);
		xnYqfujianMap.put("FJ_27", 754l);
		xnYqfujianMap.put("FJ_28", 755l);
		xnYqfujianMap.put("FJ_29", 756l);
		xnYqfujianMap.put("FJ_30", 757l);
		xnYqfujianMap.put("FJ_31", 758l);
		xnYqfujianMap.put("FJ_32", 759l);
		xnYqfujianMap.put("FJ_33", 760l);
		xnYqfujianMap.put("FJ_34", 761l);
		xnYqfujianMap.put("FJ_35", 762l);
		xnYqfujianMap.put("FJ_36", 763l);
		xnYqfujianMap.put("FJ_37", 764l);
		xnYqfujianMap.put("FJ_38", 765l);
		xnYqfujianMap.put("FJ_39", 766l);
		xnYqfujianMap.put("FJ_40", 767l);
		xnYqfujianMap.put("FJ_41", 768l);
		xnYqfujianMap.put("FJ_42", 769l);
		xnYqfujianMap.put("FJ_43", 770l);
		xnYqfujianMap.put("FJ_44", 771l);
		xnYqfujianMap.put("FJ_45", 772l);
		xnYqfujianMap.put("FJ_46", 773l);
		xnYqfujianMap.put("FJ_47", 774l);
		xnYqfujianMap.put("FJ_48", 775l);
		xnYqfujianMap.put("FJ_49", 776l);
		xnYqfujianMap.put("FJ_50", 777l);
		xnYqfujianMap.put("FJ_51", 778l);
		xnYqfujianMap.put("FJ_52", 779l);
		xnYqfujianMap.put("FJ_53", 780l);
		xnYqfujianMap.put("FJ_54", 781l);
		xnYqfujianMap.put("FJ_55", 782l);
		xnYqfujianMap.put("FJ_56", 783l);
		xnYqfujianMap.put("FJ_57", 784l);
		xnYqfujianMap.put("FJ_58", 785l);
		xnYqfujianMap.put("FJ_59", 786l);
		xnYqfujianMap.put("FJ_60", 787l);
		xnYqfujianMap.put("FJ_61", 788l);
		xnYqfujianMap.put("FJ_62", 789l);
		xnYqfujianMap.put("FJ_63", 790l);
		xnYqfujianMap.put("FJ_64", 791l);
		xnYqfujianMap.put("FJ_65", 792l);
		xnYqfujianMap.put("FJ_66", 793l);
		xnYqfujianMap.put("FJ_67", 794l);
		xnYqfujianMap.put("FJ_68", 795l);
		xnYqfujianMap.put("FJ_69", 796l);
		xnYqfujianMap.put("FJ_70", 797l);
		xnYqfujianMap.put("FJ_71", 798l);
		xnYqfujianMap.put("FJ_72", 799l);
	}

	public void readXml(String projectPath) {
		if (xnYqKeyMap == null)
			this.initxnYqMap();
		File file = new File(xnYqfilepath);
		File[] files = file.listFiles();
		if (files != null && files.length > 0) {

			for (int i = 0; i < files.length; i++) {
				if (files[i].isFile()) {
					String filePath = files[i].getPath();
					String fileName = files[i].getName();
					try {
						// 解析xml内容
						parseXMLService.initXmlFile(xnYqKeyMap, filePath,xnYqfeedbackPath, xnYqexceptionPath);
						// 解析xml头部内容
						parseXMLService.initXmlFileHead(xnYqKeyMapHead,files[i], xnYqfeedbackPath, xnYqexceptionPath);

						// 把数据部分保存到数据库
						YwYnJzfqwczzXnForm form = new YwYnJzfqwczzXnForm();
						for (String mapKey : xnYqKeyMap.keySet()) {
							
							form.setYslNo(parseXMLService.getMapValue(xnYqKeyMap.get("YSL_NO")));
							form.setSqdw(parseXMLService.getMapValue(xnYqKeyMap.get("SQDW")));
							
							YwComQyxxForm ywComQyxxForm = parseXMLService.getSqdwBySqdw(parseXMLService.getMapValue(xnYqKeyMap.get("SQDW")),"jzfqwczzxn_yq");
							if(ywComQyxxForm != null){
								form.setQyid(ywComQyxxForm.getId());
							}

							form.setAddress(parseXMLService.getMapValue(xnYqKeyMap.get("ADDRESS")));
							form.setGcdz(parseXMLService.getMapValue(xnYqKeyMap.get("GCDZ")));
							form.setGcmc(parseXMLService.getMapValue(xnYqKeyMap.get("GCMC")));
							form.setJsdwDz(parseXMLService.getMapValue(xnYqKeyMap.get("JSDW_DZ")));
							form.setJsdwLxr(parseXMLService.getMapValue(xnYqKeyMap.get("JSDW_LXR")));
							form.setJsdwLxrLxdh(parseXMLService.getMapValue(xnYqKeyMap.get("JSDW_LXR_LXDH")));
							form.setSgdwGz(parseXMLService.getMapValue(xnYqKeyMap.get("SGDW_GZ")));
							form.setSgdwXmjl(parseXMLService.getMapValue(xnYqKeyMap.get("SGDW_XMJL")));
							form.setSgdwXmjlLxdh(parseXMLService.getMapValue(xnYqKeyMap.get("SGDW_XMJL_LXDH")));
							form.setSgdwLxr(parseXMLService.getMapValue(xnYqKeyMap.get("SGDW_LXR")));
							form.setSgdwLxrLxdh(parseXMLService.getMapValue(xnYqKeyMap.get("SGDW_LXR_LXDH")));
							form.setSbjzfqwczl(parseXMLService.getMapValue(xnYqKeyMap.get("SBJZFQWCZL")));

							form.setBranch("jzfqwczzxn_yq");
							form.setApprovalNo(parseXMLService.getMapValue(xnYqKeyMap.get("approvalNo")));
							form.setBusinessCode(parseXMLService.getMapValue(xnYqKeyMap.get("businessCode")));
							String wstjsjStr = parseXMLService.getMapValue(xnYqKeyMap.get("sendTime"));
							Date wstjsj = null;
							if (StringUtils.isNotEmpty(wstjsjStr)) {
								wstjsj = DateUtils.stringToDate(wstjsjStr, DateUtils.YYYY_MM_DD_HH_MM_SS);
								form.setWstjsj(wstjsj);
							}
							form.setYsl(1l);
							form.setSendNotice("Y");// 是否发送通知书到窗口？Y，发送；其他，不发送
						}

						// 调用DAO，保存到数据库
						try {
							ywYnJzfqwczzXnyqService.save(form);
						} catch (Exception e) {
							// TODO 生成异常xml
							e.printStackTrace();
							errorCode = "1001";
							errorStatus = "插入数据库失败";
							parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
									xnYqKeyMapHead, xnYqfeedbackPath, xnYqexceptionPath, i);
						}

						//处理附件
						parseXMLService.handleFj(projectPath, fileName, xnYqfujianpath, xnYqfeedbackPath, xnYqexceptionPath, xnYqKeyMapHead,
								files, xnYqfujianMap, i, form.getQyid(), form.getId(), "jzfqwczzxn_yq");
					} catch (Exception e) {
						// TODO 生成异常xml
						e.printStackTrace();
						errorCode = "1000";
						errorStatus = "解析xml失败";
						parseXMLService.createSaveOrParseExceptionXml(errorCode, errorStatus, e, files, 
								xnYqKeyMapHead, xnYqfeedbackPath, xnYqexceptionPath, i);
					}
					// 剪切文件,把文件移走
					parseXMLService.removeXML(filePath, xnYqbackPath + fileName,fileName);
				}
			}
		}
	}
}
